cmake_minimum_required(VERSION 3.16)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32_gcc.cmake)

project(SunTracker C ASM)
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)

set(CMSIS_COMP_LIST "")
set(HAL_COMP_LIST RCC GPIO CORTEX UART IWDG TIM DMA DCMI LL_FSMC SRAM)

list(APPEND CMSIS_COMP_LIST STM32F4)
list(APPEND HAL_COMP_LIST STM32F4)
find_package(CMSIS COMPONENTS "${CMSIS_COMP_LIST}" REQUIRED)
find_package(HAL COMPONENTS "${HAL_COMP_LIST}" REQUIRED)

# 查找并添加src目录下的所有C文件
file(GLOB_RECURSE APP_SOURCE_FILES "APP/*.c")
file(GLOB_RECURSE BSP_SOURCE_FILES "BSP/*.c")
file(GLOB_RECURSE CORE_SOURCE_FILES "CORE/*.c")

# 去除BSP_SOURCE_FILES中的lcd_ex.c
list(REMOVE_ITEM BSP_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/BSP/LCD/lcd_ex.c")

include_directories(
    APP
    BSP
    BSP/OV7670
    BSP/MOTOR
    CORE
)

target_compile_definitions(STM32::F4 INTERFACE
    USE_HAL_DRIVER
    STM32F407xx
)

target_compile_options(STM32::F4 INTERFACE
    -O2
)

add_executable(${PROJECT_NAME} ${APP_SOURCE_FILES} ${BSP_SOURCE_FILES} ${CORE_SOURCE_FILES} ${MIDL_SOURCE_FILES})
target_link_libraries(${PROJECT_NAME}
    HAL::STM32::F4::RCC
    HAL::STM32::F4::GPIO
    HAL::STM32::F4::UART
    HAL::STM32::F4::IWDG
    HAL::STM32::F4::TIM
    HAL::STM32::F4::DMA
    HAL::STM32::F4::DMAEx
    HAL::STM32::F4::CORTEX
    HAL::STM32::F4::DCMI
    HAL::STM32::F4::TIMEx
    HAL::STM32::F4::SRAM
    HAL::STM32::F4::LL_FSMC
    CMSIS::STM32::F407VG
    STM32::NoSys
)

stm32_print_size_of_target(${PROJECT_NAME})
stm32_generate_binary_file(${PROJECT_NAME})
stm32_generate_hex_file(${PROJECT_NAME})

add_custom_target(clean-all
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target clean
    COMMENT "Clean all generated files"
)
